//定义电影导演相关的接口
const express = require('express')
const router = express.Router()
const response = require('../pool/Response')
//引入mysql连接池
const pool = require('../pool/pool')
//引入joi模块
const joi = require('joi')

//添加剧照
router.post('/movie-thumb/add',(req,res) => {
    let {url,movie_id} = req.body
    let schema = joi.object({
        url:joi.string().required(),
        movie_id:joi.string().required()
    })
    let {error,value} = schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('insert into movie_thumb (url,movie_id) values(?,?)',[url,movie_id],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok())
    })
})
//删除剧照
router.post('/movie-thumb/del',(req,res) => {
    let {id} = req.body
    //表单验证
    let schema = joi.object({
        id:joi.string().required()
    })
    let {error,value} =schema.validate(req.body)
    if(error){
        res.send(response.error(400,error))
        return
    }
    pool.query('delete from movie_thumb where id = ?',[id],(error,result) => {
        if(error){
            res.send(response.error(500,error))
            throw error;
        }
        res.send(response.ok())
    })
})


//通过电影id查询剧照
router.get('/movie-thumbs/movieid',(req,res) => {
    let {movie_id} = req.query
    let schema = joi.object({
        movie_id:joi.string().required(),
    })
   let {error,value} = schema.validate(req.query)
    if(error){
        res.send(response.error(400,error))
        return;
    }
    pool.query('select * from movie_thumb where movie_id=?',[movie_id],(err,result)=>{
        if(err){
            res.send(response.error(500,err))
            throw err;
        }
        res.send(response.ok(result))
    })
})


module.exports = router